//
//  WTCommentViewController.m
//  OneDay
//
//  Created by lanou on 16/1/13.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTCommentViewController.h"
#import "WTCommentTableViewCell.h"
#import "WTRequestManager.h"
#import "WTRecommendListModel.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>

@interface WTCommentViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView * tableView;

// 热门评论的数组
@property (nonatomic, strong) NSMutableArray * commentListArray;

// 从第几个开始网络请求
@property (nonatomic, assign) NSInteger requestStart;

// 等待网络请求完成
@property (nonatomic, strong) MBProgressHUD * hud;
@end

@implementation WTCommentViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    _requestStart = 1;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 初始化tableView
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64 - 49) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    // 注册cell
    [_tableView registerClass:[WTCommentTableViewCell class] forCellReuseIdentifier:@"WTCommentTableViewCell"];
    
    // 创建多少个评论的按钮
    NSString * title = [NSString stringWithFormat:@"%@条评论",_commentNumber];
    UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithTitle:title style:(UIBarButtonItemStylePlain) target:self action:@selector(rightButtonClick:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    
    _commentListArray = [NSMutableArray array];
    [self requestData];
    
    
    
    // 明杰刷新
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _requestStart = 1;
        [self requestData];
    }];
    
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _requestStart++;
        [self requestData];
    }];
    
    [self setupprogressHud];
    // Do any additional setup after loading the view.
}

// rightButton 导航栏右侧的按钮
- (void)rightButtonClick:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- 等待网络完成
- (void)setupprogressHud
{
    _hud = [[MBProgressHUD alloc]initWithView:self.view];
    _hud.frame = self.view.bounds;
    _hud.minSize = CGSizeMake(100, 100);
    _hud.mode = MBProgressHUDModeDeterminate;
    _hud.labelText = @"正在努力加载....";
    [self.view addSubview:_hud];
    
    [_hud show:YES];
    
}
#pragma mark -- 评论的网络请求
- (void)requestData
{
    NSString * url = [NSString stringWithFormat:@"http://api.wap.miercn.com/api/2.0.3/feedback.php?act=get_list&aid=%@&page=%ld&plat=android&proct=mierapp&versioncode=20150807&apiCode=4",_ID,(long)_requestStart];
    [WTRequestManager requestWithUrlString:url parDic:nil method:GET success:^(NSData *data) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        // 解析全部评论
        NSArray * listArray = dic[@"commentList"];
        
        
        if (_requestStart == 1) {
            [_commentListArray removeAllObjects];
        }
        
        for (NSDictionary *oneDic in listArray) {
            WTRecommendDetailModel *model = [[WTRecommendDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_commentListArray addObject:model];
        }
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        [_hud hide:YES];
        
    } error:^(NSError *error) {
    }];
}



#pragma mark -- dataSource的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_hotCommentArray.count == 0) {
        return 1;
    }else {
       return 2;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_hotCommentArray.count == 0) {
        return _commentListArray.count;
    } else {
        if (section == 0) {
            return _hotCommentArray.count;
        } else {
            return _commentListArray.count;
        }
    }
  

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WTCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WTCommentTableViewCell" forIndexPath:indexPath];
    // 判断是热门还是
    if (indexPath.section == 0 && _hotCommentArray.count != 0) {
       WTRecommendDetailModel *model = _hotCommentArray[indexPath.row];
       cell.model = model;
    } else{
        WTRecommendDetailModel *model = _commentListArray[indexPath.row];
        cell.model = model;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}
// 返回分区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0 && _hotCommentArray.count != 0) {
        return @"精彩评论";
    } else{
        return @"全部评论";
    }
    
}

// 返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // indexPath.section == 0 代表的是精彩评论, 1代表的是全部评论,这里返回cell的高度
    if (indexPath.section == 0 && _hotCommentArray.count != 0) {
        return [WTCommentTableViewCell heightForRowWithModel:_hotCommentArray[indexPath.row]];
    }else {
       return [WTCommentTableViewCell heightForRowWithModel:_commentListArray [indexPath.row]];
    }
   
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
